//
//  DLFormScrollView.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/11/30.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLFormScrollView.h"
#import "DLMachineModel.h"
#import "DLMachineFormContentTC.h"
#import "DLShowFormWholeContentVC.h"

@interface DLFormScrollView()

@property (nonatomic, strong) NSMutableArray *formContentTableDataSource;

@end

@implementation DLFormScrollView


- (instancetype) initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.formContentTableDataSource = [NSMutableArray array];
        [self layoutSubControls];
    }
    return self;
}
- (void) layoutSubControls {
    UIView *rowTitleBgView = [UIView new];
    rowTitleBgView.frame = CGRectMake(0, 0, self.width, kMachineFormTitleLabelHeight);
    rowTitleBgView.backgroundColor = [UIColor blackColor];
    rowTitleBgView.backgroundColor = kColorRGB(46, 56, 70);
    rowTitleBgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:rowTitleBgView];
    self.rowTitleBgView = rowTitleBgView;
    
    UITableView *formContentTableView = [[UITableView alloc] initWithFrame:CGRectMake(rowTitleBgView.left, rowTitleBgView.bottom, rowTitleBgView.width, kMachineFormTableViewHeight) style:UITableViewStylePlain];
    formContentTableView.delegate = self;
    formContentTableView.dataSource = self;
//    formContentTableView.showsVerticalScrollIndicator = NO;
//    formContentTableView.showsHorizontalScrollIndicator = NO;
    formContentTableView.scrollEnabled = NO;
    formContentTableView.rowHeight = kMachineFormTableViewRowHeight;
    formContentTableView.backgroundColor = [UIColor blackColor];
    formContentTableView.backgroundColor = kColorRGB(46, 56, 70);
    formContentTableView.backgroundColor = [UIColor whiteColor];
    [self addSubview:formContentTableView];
    self.formContentTableView = formContentTableView;
    [formContentTableView registerClass:[DLMachineFormContentTC class] forCellReuseIdentifier:@"DLMachineFormContentTC"];
    formContentTableView.separatorColor = kColorRGB(232, 232, 232);
    
    if ([formContentTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [formContentTableView setSeparatorInset:UIEdgeInsetsZero];
    }
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.formContentTableDataSource.count;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DLMachineFormContentTC *machineFormContentTC = [tableView dequeueReusableCellWithIdentifier:@"DLMachineFormContentTC"];
    machineFormContentTC.formContentDataSource = self.formContentTableDataSource[indexPath.row];
    machineFormContentTC.labelTapBlock = ^(UILabel * _Nonnull tapLabel) {
        
//        CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];
//
//
//        CGRect rect = [tableView convertRect:rectInTableView toView:tableView.superview.superview];
//
//        if (self.contentTCTapLabelBlock) {
//            self.contentTCTapLabelBlock(tapLabel, rect);
//        };
        
        NSDictionary *dic = self.formScrollViewModel.resultDic[@"data"];
        NSArray *titArr = dic[@"ColLabel"];
        
        NSString *title = [NSString stringWithFormat:@"%@", titArr[tapLabel.tag - 1000]];
        
        DLShowFormWholeContentVC *sfwcvc = [DLShowFormWholeContentVC new];
        sfwcvc.showTitle = title;
        sfwcvc.wholeContent = tapLabel.text;
        [kuserManager.lastVc.navigationController pushViewController:sfwcvc animated:YES];
        
    };
    return machineFormContentTC;
}

- (void) setFormScrollViewModel:(DLMachineModel *)formScrollViewModel {
    _formScrollViewModel = formScrollViewModel;
    
    NSDictionary *dic = self.formScrollViewModel.resultDic[@"data"];
    //title
    NSArray *titArr = dic[@"ColLabel"];
    
    if (titArr.count > 1) {
        
        [self.rowTitleBgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        
        CGFloat titleLabelWidth = kMachineFormContentTitleLabelWidth;
        
        for (NSInteger i = 1; i < titArr.count; i++) {
            UILabel *titleLabel = [UILabel new];
            titleLabel.frame = CGRectMake(titleLabelWidth * (i - 1), 0, titleLabelWidth, kMachineFormTitleLabelHeight);
            titleLabel.textColor = kDLNormalGrayColor;
            titleLabel.textAlignment = NSTextAlignmentCenter;
            titleLabel.font = kSystemFont(12);
            [self.rowTitleBgView addSubview:titleLabel];
            titleLabel.backgroundColor = [UIColor blackColor];
            titleLabel.backgroundColor = kColorRGB(46, 56, 70);
            titleLabel.backgroundColor = [UIColor whiteColor];
            titleLabel.text = [NSString stringWithFormat:@"%@", titArr[i]];
        }
    }
    
    //content
    [self.formContentTableView resetWidth:self.contentSize.width];
    NSArray *contentArr = dic[@"kData"];
    if (contentArr.count > 1) {
        [self.formContentTableDataSource removeAllObjects];
        
        //这里判断如果图表只有8条以下的数据，就全部展示 不需要展开收起功能 同时隐藏展开收起按钮
        if (contentArr.count < 9) {
            
            for (NSInteger i = 0; i < contentArr.count; i++) {
                
                NSArray *arr = contentArr[i];
                [self.formContentTableDataSource addObject:arr];
            }
            
            [self.formContentTableView resetHeight:contentArr.count *kMachineFormTableViewRowHeight + kMachineFormTableViewRowHeight];
            
        } else {
            for (NSInteger i = 0; i < contentArr.count; i++) {
                
                NSArray *arr = contentArr[i];
                //如果是展开的，那数据源就保留全部数据,如果是收起的，那就展示5条数据
                if (i == 5) {
                    if (formScrollViewModel.isFormShowAllData == YES) {
                        [self.formContentTableDataSource addObject:arr];
                        continue;
                    } else {
                        break;
                    }
                }
                [self.formContentTableDataSource addObject:arr];
            }
            
            [self.formContentTableView resetHeight:contentArr.count *kMachineFormTableViewRowHeight + kMachineFormTableViewRowHeight];
        }
        [self.formContentTableView reloadData];
    }
}


@end
